/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DriverPanel.java
 *
 * Created on Dec 17, 2011, 4:13:28 PM
 */
package otobustakipsistemi;

import DatabaseConnection.EmployeeDbConnection;
import Entities.Employee;
import java.sql.ResultSetMetaData;
import java.sql.ResultSet;
import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory.Default;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author serhat
 */
public class EmployeePanel extends javax.swing.JPanel {

    EmployeeDbConnection employeeDbConnection;

    /** Creates new form DriverPanel */
    public EmployeePanel() {
        initComponents();
    }

    private void errorMessage(String message) {
        JOptionPane.showMessageDialog(this, message, "Hata", JOptionPane.ERROR_MESSAGE);
    }

    /** This method is called from within the constructor to
     * initiaWIDTHlize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel11 = new javax.swing.JLabel();
        tc = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        nameSearch = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        lastNameSearch = new javax.swing.JTextField();
        search = new javax.swing.JButton();
        saveChanges = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        tcNo = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        name = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        lastName = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        mobilePhone = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        phone = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        salary = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        hour = new javax.swing.JTextField();
        state = new javax.swing.JCheckBox();
        position = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        save = new javax.swing.JButton();
        birthDate = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();

        jLabel2.setText("jLabel2");

        setLayout(new java.awt.BorderLayout());

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "TC", "Ad", "Soyad", "Dogum Tarihi",
                "Cep Telefonu", "Sabit Telefon", "Maaş",
                "Hafalik Calisma Saati", "Durum", "Görev"
            }
        ));
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);

        jLabel11.setText("Tc Kimlik No ");

        jLabel13.setText("Ad");

        jLabel14.setText("Soyad");

        search.setText("Ara");
        search.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchActionPerformed(evt);
            }
        });

        saveChanges.setText("Degisiklikleri Kaydet");
        saveChanges.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveChangesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(search, javax.swing.GroupLayout.DEFAULT_SIZE, 469, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(12, 12, 12)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(tc, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
                                .addComponent(nameSearch, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(28, 28, 28)
                                .addComponent(jLabel12)))
                        .addGap(29, 29, 29)
                        .addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lastNameSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(95, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(saveChanges, javax.swing.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 576, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(tc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addGap(9, 9, 9)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(nameSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lastNameSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3)
                .addComponent(search)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 412, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(saveChanges)
                .addContainerGap(562, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Personel Ara", jPanel2);

        jPanel1.setLayout(null);

        jLabel1.setText("Tc Kimlik No");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(50, 30, 150, 30);
        jPanel1.add(tcNo);
        tcNo.setBounds(200, 30, 180, 30);

        jLabel3.setText("Ad");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(50, 70, 150, 30);
        jPanel1.add(name);
        name.setBounds(200, 70, 180, 30);

        jLabel4.setText("Soyad");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(50, 110, 150, 30);
        jPanel1.add(lastName);
        lastName.setBounds(200, 110, 180, 30);

        jLabel5.setText("Cep Telefonu");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(50, 150, 150, 30);
        jPanel1.add(mobilePhone);
        mobilePhone.setBounds(200, 150, 180, 30);

        jLabel6.setText("Sabit Telefon");
        jPanel1.add(jLabel6);
        jLabel6.setBounds(50, 190, 150, 30);
        jPanel1.add(phone);
        phone.setBounds(200, 190, 180, 30);

        jLabel7.setText("Maas");
        jPanel1.add(jLabel7);
        jLabel7.setBounds(50, 230, 150, 30);
        jPanel1.add(salary);
        salary.setBounds(200, 230, 180, 30);

        jLabel8.setText("Calisma Saati");
        jPanel1.add(jLabel8);
        jLabel8.setBounds(50, 270, 150, 30);
        jPanel1.add(hour);
        hour.setBounds(200, 270, 180, 30);

        state.setText("Uygun");
        jPanel1.add(state);
        state.setBounds(200, 390, 180, 23);

        position.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Şöför", "Muavin", "Bilet Satıcısı"}));
        position.setSelectedIndex(-1);
        jPanel1.add(position);
        position.setBounds(200, 310, 180, 24);

        jLabel9.setText("Görev");
        jPanel1.add(jLabel9);
        jLabel9.setBounds(50, 310, 150, 30);

        save.setText("Kaydet");
        save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });
        jPanel1.add(save);
        save.setBounds(200, 430, 180, 25);
        jPanel1.add(birthDate);
        birthDate.setBounds(200, 350, 180, 30);

        jLabel10.setText("Dogum Tarihi");
        jPanel1.add(jLabel10);
        jLabel10.setBounds(50, 350, 150, 30);

        jTabbedPane1.addTab("Personel Ekle", jPanel1);

        add(jTabbedPane1, java.awt.BorderLayout.PAGE_START);
    }// </editor-fold>//GEN-END:initComponents

private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
    try {
        employeeDbConnection = new EmployeeDbConnection();
        Employee employee = new Employee();

        employee.setTcNo(Long.valueOf(tcNo.getText()));
        employee.setName(name.getText());
        employee.setLastName(lastName.getText());
        employee.setMobilePhone(mobilePhone.getText());
        employee.setPhone(phone.getText());
        employee.setSalary(Float.valueOf(salary.getText()));
        employee.setHour(Integer.valueOf(hour.getText()));
        employee.setBirthdate(birthDate.getText());

        int selected = 0;
        if (state.isSelected()) {
            selected = 1;
        }

        employee.setState(selected);
        employee.setPosition((String) position.getSelectedItem());

        boolean isOk = employeeDbConnection.insert(employee);

        if (isOk) {
            JOptionPane.showMessageDialog(this, "Personel sisteme eklendi.", "Bilgi", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(this, "Personel sisteme eklenemedi !", "Hata", JOptionPane.ERROR_MESSAGE);
        }

    } catch (ClassNotFoundException ex) {
        errorMessage(ex.toString());
    } catch (SQLException ex) {
        errorMessage(ex.toString());
    } catch (InstantiationException ex) {
        errorMessage(ex.toString());
    } catch (IllegalAccessException ex) {
        errorMessage(ex.toString());
    }
}//GEN-LAST:event_saveActionPerformed

private void searchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchActionPerformed
    try {
        long TC = 0;
        if (!tc.getText().equals("")) {
            TC = Long.parseLong(tc.getText());
        }
        Employee employee = new Employee(TC,
                nameSearch.getText(),
                lastNameSearch.getText());
        employeeDbConnection = new EmployeeDbConnection();
        ResultSet rs = employeeDbConnection.select(employee);
        DefaultTableModel model = new DefaultTableModel();
        String[] tableColumnsName = {
            "TC", "Ad", "Soyad", "Dogum Tarihi",
            "Cep Telefonu", "Sabit Telefon", "Maaş",
            "Hafalik Calisma Saati", "Durum", "Görev"
        };
        model.setColumnIdentifiers(tableColumnsName);
        ResultSetMetaData metaData = rs.getMetaData();
        int columns = metaData.getColumnCount();
        while (rs.next()) {
            Object objects[] = new Object[columns];
            for (int i = 0; i < columns; i++) {
                objects[i] = rs.getObject(i + 1);
            }
            model.addRow(objects);
        }

        jTable1.setModel(model);


    } catch (ClassNotFoundException ex) {
        errorMessage(ex.toString());
    } catch (SQLException ex) {
        errorMessage(ex.toString());
    } catch (InstantiationException ex) {
        errorMessage(ex.toString());
    } catch (IllegalAccessException ex) {
        errorMessage(ex.toString());
    }
}//GEN-LAST:event_searchActionPerformed
    private class Worker extends SwingWorker<String, Object> {

        private TableModel model;

        public Worker() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
            model = jTable1.getModel();
            employeeDbConnection = new EmployeeDbConnection();
        }

        @Override
        protected String doInBackground() throws Exception {
            System.out.println(model.getRowCount());
            for (int i = 0; i < model.getRowCount(); i++) {
                Employee employee = new Employee();
                
                employee.setTcNo(Long.valueOf(model.getValueAt(i, 0).toString()));
                employee.setName( model.getValueAt(i, 1).toString());
                employee.setLastName(model.getValueAt(i, 2).toString());
                employee.setBirthdate(model.getValueAt(i, 3).toString());
                employee.setMobilePhone( model.getValueAt(i, 4).toString());
                employee.setPhone(model.getValueAt(i, 5).toString());
                employee.setSalary(Float.valueOf(model.getValueAt(i, 6).toString()));
                employee.setHour(Integer.valueOf(model.getValueAt(i, 7).toString()));
                int temp = 0;
                if(model.getValueAt(i, 8).toString().equals("true")){
                    temp = 1;
                }
                employee.setState(temp);
                employee.setPosition(model.getValueAt(i, 9).toString());
                
                employeeDbConnection.update(employee);
                
                
            }
            return null;
        }
    }
private void saveChangesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveChangesActionPerformed
    Worker worker;
    try {
        worker = new Worker();
        worker.execute();
    } catch (ClassNotFoundException ex) {
        errorMessage(ex.toString());
    } catch (SQLException ex) {
        errorMessage(ex.toString());
    } catch (InstantiationException ex) {
        errorMessage(ex.toString());
    } catch (IllegalAccessException ex) {
        errorMessage(ex.toString());
    }
    
}//GEN-LAST:event_saveChangesActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField birthDate;
    private javax.swing.JTextField hour;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField lastName;
    private javax.swing.JTextField lastNameSearch;
    private javax.swing.JTextField mobilePhone;
    private javax.swing.JTextField name;
    private javax.swing.JTextField nameSearch;
    private javax.swing.JTextField phone;
    private javax.swing.JComboBox position;
    private javax.swing.JTextField salary;
    private javax.swing.JButton save;
    private javax.swing.JButton saveChanges;
    private javax.swing.JButton search;
    private javax.swing.JCheckBox state;
    private javax.swing.JTextField tc;
    private javax.swing.JTextField tcNo;
    // End of variables declaration//GEN-END:variables
}
